home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1998 May: Tool Chest / Dev.CD May 98 TC.toast / Tool Chest / Development Kits / HyperCard Related / APDA HyperCard Toolkits / CD Audio Toolkit 1.0 / Source / CDTrackTitle.c < prev    next >
Encoding:
C/C++ Source or Header  |  1995-02-07  |  6.3 KB  |  236 lines  |  [TEXT/MPS ]

  1. /*
  2.     CDTrackTitle - An XFCN to report the title of the track, if one has been entered
  3.     ©Apple Computer, Inc. 1988
  4.     All Rights Reserved.
  5.     
  6.     88/11/08    BL°B    First Version
  7.  
  8.     To compile and link this file using Macintosh Programmer's Workshop,
  9.  
  10.     C CDTrackTitle.c
  11.     link -sn Main=CDTrackTitle -sn STDIO=CDTrackTitle ∂
  12.          -sn INTENV=CDTrackTitle -rt XFCN=42 ∂
  13.          -m CDTrackTitle CDTrackTitle.c.o "{CLibraries}"CRuntime.o ∂
  14.          "{CLibraries}"StdCLib.o ∂
  15.          -o HyperCommands
  16.          
  17.     This link directive puts the XCMD in the file "HyperCommands".
  18.     Substitute the name of the stack you want it in.  To move XCMDs
  19.     between stacks, use ResEdit.  They can be in an individual stack,
  20.     the Home stack, the HyperCard application, or the System File.
  21.     
  22. */
  23.  
  24. #include <cd.h>
  25. #include <ToolUtils.h>
  26.  
  27. /* prototype definitions for functions */
  28. OSErr    ReadQ(short, short *, short *, short *, short *);
  29. OSErr    FindTrackTitle(unsigned long, short, Str255    *);
  30.  
  31. /* **** WARNING:  DO NOT USE GLOBAL VARIABLES! **** */
  32.  
  33.  
  34. /************************************************************************
  35.  *
  36.  *  Function:        CDTrackTitle
  37.  *
  38.  *  Purpose:        return the title of this track.
  39.  *
  40.  *  Returns:        a string, or empty
  41.  *
  42.  *  Side Effects:
  43.  *
  44.  *  Description:    We need no parameters:
  45.  *                    Get the ioRefNum that we got from previously calling
  46.  *                    CDOpen() by accessing the famous global
  47.  *                    call the driver with a READTOC call to find out
  48.  *                    the lead-out time.  This is a unique number for each
  49.  *                    disc.  Look in the System Folder for the file
  50.  *                    "CD Remote Programs".  Open that file, look at the
  51.  *                    IndX resource to find out if we've got a matching
  52.  *                    index.  If we do, look up the STR# resource 
  53.  *                    referenced and return the first STR from that list.
  54.  *
  55.  *    88 11 14    BL°B    Add the ability to take an optional parameter
  56.  *                        so that we look for the title of THAT track
  57.  *                        instead.
  58.  *
  59.  ************************************************************************/
  60. pascal void
  61. CDTrackTitle(paramPtr)
  62. XCmdBlockPtr    paramPtr;
  63. {
  64.     OSErr    result;
  65.     short    ioRefNum;
  66.     Handle    refHandle;
  67.     unsigned long    discID;
  68.     short    trackNo, minute, second, block;
  69.     Str255    title;
  70.     Str31    returnString;
  71.     
  72.     trackNo = 0;
  73.     strcpy(title, "\p");
  74.     
  75.     /* Must be no more than 1 parameter */
  76.     if ((paramPtr->paramCount) > 1)
  77.     {
  78.         /* Report error in parameters by returning -1 */
  79.         NumToStr(paramPtr, (long) -1, &returnString);
  80.         paramPtr->returnValue = PasToZero(paramPtr, (StringPtr) &returnString);
  81.         return;
  82.     }
  83.  
  84.     /* Get the global ioRefNum and convert it. */
  85.     refHandle = GetGlobal(paramPtr, GLOBALNAME);
  86.     ioRefNum = atoi(*(refHandle));
  87.     DisposHandle(refHandle);
  88.     ioRefNum &= 0xFFFF;            /* remove vRefNum; not needed. */
  89.  
  90.     result = IDDisc(ioRefNum, &discID);
  91.     
  92.     if (discID == 0)
  93.         return;        /* empty string return if we can't ID the disc */
  94.  
  95.     if (result == noErr)
  96.         result = ReadQ(ioRefNum, &trackNo, &minute, &second, &block);
  97.  
  98. /* If we were passed a track number, use it instead of the current track */
  99.     if ((paramPtr->paramCount) == 1)
  100.         trackNo = atoi((char *)*(paramPtr->params[0]));
  101.     
  102.     if (result == noErr)
  103.         result = FindTrackTitle(discID, trackNo, &title);
  104.     
  105.     paramPtr->returnValue = PasToZero(paramPtr, (StringPtr) &title);
  106. }
  107.  
  108.  
  109.  
  110. /************************************************************************
  111.  *
  112.  *  Function:        ReadQ
  113.  *
  114.  *  Purpose:        return current Q subcode address data
  115.  *
  116.  *  Returns:        OSErr.  Probably either
  117.  *                        noErr        everything's hunky-dory!
  118.  *                        paramErr    you messed up the call somehow.
  119.  *
  120.  *  Side Effects:    none
  121.  *
  122.  *  Description:    Simply call the driver.  Return track number and
  123.  *                    absolute minute, second, block.
  124.  *
  125.  ************************************************************************/
  126. OSErr
  127. ReadQ(refNum, trackNo, minute, second, block)
  128. short    refNum;
  129. short    *trackNo;
  130. short    *minute;
  131. short    *second;
  132. short    *block;
  133. {
  134.     CDParam    myPB;
  135.     OSErr    result;
  136.     
  137.     myPB.ioCompletion = 0;
  138.     myPB.ioNamePtr = (char *) 0;
  139.     myPB.ioVRefNum = 1;
  140.     myPB.ioCRefNum = refNum;
  141.     myPB.csCode = READQ;
  142.     
  143.     result = PBControl(&myPB, false);
  144.     
  145.     if (result == noErr)
  146.     {
  147.         *trackNo = (short) BCD2DECIMAL(myPB.csParam[1]);
  148.         *minute = (short) BCD2DECIMAL(myPB.csParam[6]);
  149.         *second = (short) BCD2DECIMAL(myPB.csParam[7]);
  150.         *block = (short) BCD2DECIMAL(myPB.csParam[8]);
  151.     }
  152.     return result;
  153. }
  154.  
  155.  
  156.  
  157. /************************************************************************
  158.  *
  159.  *  Function:        FindTrackTitle
  160.  *
  161.  *  Purpose:        Find track title, given the disc ID
  162.  *
  163.  *  Returns:        OSErr.  Usually noErr, but could have an error
  164.  *                    in opening the CD Remote Programs file.
  165.  *
  166.  *  Side Effects:    fills in title with found title
  167.  *
  168.  *  Description:    open the file "CD Remote Programs".  Look at the
  169.  *                    'IndX' resource to find out if we have a title.
  170.  *                    If we can't find the appropriate 'IndX' resource,
  171.  *                    set the title to blank and return.  If we find
  172.  *                    the appropriate IndX resource, open the 'ProG'
  173.  *                    resource associated with this disc.  Loop through
  174.  *                    the 'ProG' resource to find where the track title
  175.  *                    is located (the user may have used the CD Remote
  176.  *                    desk accessory to change the order of the tracks.)
  177.  *                    When you find the appropriate string number, use
  178.  *                    GetIndString() to get the STR# string associated
  179.  *                    with this track.
  180.  *
  181.  ************************************************************************/
  182. OSErr
  183. FindTrackTitle(discID, trackNo, title)
  184. unsigned long    discID;
  185. short            trackNo;
  186. Str255            *title;
  187. {
  188.     OSErr    result;
  189.     short    rsrcID;            /* holds the 'STR#' & 'ProG' resource id. */
  190.     short    resFile;
  191.     short    *prog;
  192.     Handle    progHandle;
  193.     short    limit;
  194.     short    realTrackNo;        /* 'ProG' may have different sequence */
  195.     Str255    fileName;
  196.     
  197.     result = noErr;
  198.     
  199.     GetIndString(fileName, STR_ID, DRIVENAME);
  200.     if (fileName == (Str255)0)
  201.         result = ResError();
  202.  
  203.     if (result == noErr)
  204.     {
  205.         resFile = OpenSystemResFile(fileName);
  206.         if (resFile == -1)
  207.             result = ResError();
  208.     }
  209.     
  210.     
  211.     if (result == noErr)
  212.         if (FindIndex(discID, &rsrcID) == true)
  213.         {
  214.             progHandle = Get1Resource('ProG', rsrcID);
  215.             if (progHandle == nil)
  216.                 result = ResError();
  217.             if (result == noErr)
  218.             {
  219.                 prog = (short *) *progHandle;
  220.                 limit = prog[0];
  221.                 for (realTrackNo = 1; realTrackNo <= limit; realTrackNo++)
  222.                 {
  223.                     if (BCD2DECIMAL(prog[realTrackNo] & 0x00FF) == trackNo)
  224.                         GetIndString(title, rsrcID, realTrackNo+1);
  225.                 }
  226.             }
  227.         }
  228.     
  229.     CloseResFile(resFile);
  230.     return result;
  231. }
  232.  
  233.  
  234. /* C routines for HyperCard callbacks */
  235. #include <XCmdGlue.inc.c>
  236.